package com.wuxf.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wuxf.entity.ResponseBodyEntity;
import com.wuxf.entity.UserInfo;
import com.wuxf.entity.custom.UserInfoCustom;
import com.wuxf.service.UserService;

@Controller
@SuppressWarnings({"rawtypes","unchecked"})
public class UserController {

	@Autowired
	private UserService userService;
	
	
	@RequestMapping("/goToUpdatePwd")
	public String goToUpdatePwd(HttpSession session) {
		return "updatePwd";
	}

	@RequestMapping("user/updatePwd")
	public @ResponseBody ResponseBodyEntity updatePwd(HttpSession session, String oldPwd,String newPwd) {
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		UserInfoCustom userInfoCustom = null;
		if(session.getAttribute("userInfoCustom") != null) {
			userInfoCustom = (UserInfoCustom)session.getAttribute("userInfoCustom");
		}
		if(userInfoCustom.getPassWord().equals(oldPwd)) {
			UserInfo userInfo = new UserInfo();
			BeanUtils.copyProperties(userInfoCustom, userInfo);
			userInfo.setPassWord(newPwd);
			userService.updateUser(userInfo);
			responseBodyEntity.setData(userInfo);
			responseBodyEntity.setState("1");
		}else {
			responseBodyEntity.setMsg("原密码错误!");
			responseBodyEntity.setState("0");
		}
		return responseBodyEntity;
	}
	
	
	@RequestMapping("/login")
	public @ResponseBody ResponseBodyEntity goToIndex(HttpSession session, String userAccount,String passWord) {
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		HashMap paraMap = new HashMap();
		paraMap.put("userAccount", userAccount);
		paraMap.put("passWord", passWord);
		List<UserInfoCustom> userInfoCustoms = userService.findByMap(paraMap);
		if(userInfoCustoms!=null && userInfoCustoms.size()>0) {
			responseBodyEntity.setData(userInfoCustoms.get(0));
			responseBodyEntity.setState("1");
			session.setAttribute("userInfoCustom", userInfoCustoms.get(0));
		}else {
			responseBodyEntity.setMsg("用户名密码不存在!");
			responseBodyEntity.setState("0");
		}
		return responseBodyEntity;
	}
	@RequestMapping("/appLogin")
	public @ResponseBody ResponseBodyEntity appLogin(String userAccount,String passWord) {
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		HashMap paraMap = new HashMap();
		paraMap.put("userAccount", userAccount);
		paraMap.put("passWord", passWord);
		List<UserInfoCustom> userInfoCustoms = userService.findByMap(paraMap);
		if(userInfoCustoms!=null && userInfoCustoms.size()>0) {
			responseBodyEntity.setData(userInfoCustoms.get(0));
			responseBodyEntity.setState("1");
		}else {
			responseBodyEntity.setMsg("用户名密码不存在!");
			responseBodyEntity.setState("0");
		}
		return responseBodyEntity;
	}
	
	@RequestMapping("/quit")
	public String goToInquitdex(HttpSession session) {
		session.removeAttribute("userInfoCustom");
		return "login";
	}
	
	
	
	
}
